package com.oceansoft.mobile.econsole.modules.weixin.controller;

import com.oceansoft.mobile.econsole.common.constant.ConfigInfo;
import com.oceansoft.mobile.econsole.common.util.FileUtils;
import com.oceansoft.mobile.econsole.common.util.FtpBusiness;
import com.oceansoft.mobile.econsole.modules.accident.entity.AccidentPhoto;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.weixin.entity.ComPlaintReportAtt;
import com.oceansoft.mobile.econsole.modules.weixin.persistence.WeixinQueryMapper;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by zeng on 2016/8/18.
 */
@Controller
@RequestMapping("upload")
public class UploadController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(UploadController.class);

    @Resource
    protected ConfigInfo configInfo;

    @Resource
    private WeixinQueryMapper weixinQueryMapper;

    /**
     * 上传文件到FTP服务器
     */
    @RequestMapping(value = "uploadAttachment",method = POST)
    @ResponseBody
    public void uploadAttachment(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String guid = request.getParameter("guid").toString();
        String lx = request.getParameter("lx").toString();

        List<MultipartFile> files =  ((MultipartHttpServletRequest)request).getFiles("uploadfiles");
        for (int i = 0; i < files.size(); i++) {
            MultipartFile multipartFile = files.get(i);
            InputStream inputStream = multipartFile.getInputStream();
            String fileName = multipartFile.getOriginalFilename();

            //图片大于2M，则压缩图片
            File parentFile = new File(configInfo.getFileUpload());
            if(!parentFile.exists()){
                parentFile.mkdirs();
            }
            File file = new File(parentFile,fileName);
            long size = multipartFile.getSize();
            if(size > (2*1024*1024)){
                Thumbnails.of(inputStream).scale(0.2f).toFile(file);
            }else{
                Thumbnails.of(inputStream).scale(1f).toFile(file);
            }

//            Date today = new Date();
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            //String path = File.separator + lx + File.separator + sdf.format(today);
            String time2 = new SimpleDateFormat("yyyyMM").format(new Date());

            //保存文件到数据库表
            ComPlaintReportAtt comPlaintReportAtt = new ComPlaintReportAtt();
            String comPlaintReportAttguid = UUID.randomUUID().toString().replace("-","");
            comPlaintReportAtt.setGuid(comPlaintReportAttguid);
            comPlaintReportAtt.setFguid(guid);
            comPlaintReportAtt.setFsize(file.length());
            comPlaintReportAtt.setAttachtype(0);
            comPlaintReportAtt.setFname(fileName);
            comPlaintReportAtt.setSavetype(1);

            String time = weixinQueryMapper.getTime();
            String yearAndMon = time.substring(0,6);
            String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
            String ftpFileName = comPlaintReportAttguid + "_" + yearAndMon + suffix;
            comPlaintReportAtt.setAttachpath(String.format("%s\\%s", time, ftpFileName));
            weixinQueryMapper.saveComPlaintReportAtt(comPlaintReportAtt);

            //上传的到指定FTP服务器
            FileInputStream input = new FileInputStream(file);

            String host = configInfo.getFileServerHost();
            int port = Integer.parseInt(configInfo.getFileServerPort());
            String username = configInfo.getFileServerUsername();
            String password = configInfo.getFileServerPassword();
            FileUtils.uploadFile(host,port,username,password,"DATA/" + time2,ftpFileName,input);
        }
    }
}
